<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class NguoidungNhomsController extends AppController {
    var $name = "NguoidungNhoms";
    var $helpers = array('Html','Session');
    // var $components = array( 'Session', 'Common', 'Auth');
    var $components = array( 'Session', 'Common');
    function beforeFilter(){
        // parent::beforeFilter();
        $controller = $this->params['controller'];
        $action = $this->params['action'];
        if(isset($controller)){
            $strControllerName =  "<a href='/admin/" .$controller. "'>Phân quyền người dùng vào nhóm</a>";
        }
        if(isset($action)){
            if($action == "admin_them"){
                $strActionName = " » Thêm dịch vụ";
            }elseif($action == "admin_sua"){
                $strActionName = " » Chỉnh sửa dịch vụ";
            }
            elseif ($action == "admin_xoa") {
                $strActionName = " » Xóa dịch vụ";
            }else{
               $strActionName = "";
            }
        }

        //Add breadcrumb
        $route = $strControllerName. $strActionName;

        $status = array(
            'hoatdong' => 'Hoạt động',
            'vohieu' => 'Vô hiệu'
        );

        $this->set('title_for_layout','Quản trị dịch vụ');
        $this->set('strControllerName',$controller);
        $this->set('status', $status);
        $this->set('route', $route);
        $this->set('group_system', 1);
    }

    public function admin_index(){
        if( !empty($this->data) )
        {
            $del_per = true;
            $add_per = true;
            if( is_array( $this->data['NguoidungNhom']['chuaphanquyen'] ))
            {
                foreach( $this->data['NguoidungNhom']['chuaphanquyen'] as $value )
                {
                    $condition = array(
                        'nguoidungid' => $value,
                        'nhomid'  => $this->data['NguoidungNhom']['nhomid']
                    );
                    if( $this->NguoidungNhom->deleteAll($condition, false ) ){
                        $del_per = true;
                    }else{
                        $del_per = false;
                    }

                }
            }
            if(is_array( $this->data['NguoidungNhom']['phanquyen'] ))
            {
                foreach( $this->data['NguoidungNhom']['phanquyen'] as $value1 )
                {
                    $save['nguoidungid'] = $value1;
                    $save['nhomid'] = $this->data['NguoidungNhom']['nhomid'];
                    $save['ngaytao'] = date('Y-m-d H:i:s');
                    $save['ngaysua'] = date('Y-m-d H:i:s');
                    $save['nguoitao'] = $this->Session->read('nguoidung_id');
                    $save['nguoisua'] = $this->Session->read('nguoidung_id');
                    $save['trangthai'] = 'hoatdong';
                    $this->NguoidungNhom->create();
                    $saveAll['NguoidungNhom'] = $save;
                    if( $this->NguoidungNhom->save($saveAll, array( 'validate' => false))){
                        $add_per = true;
                    }else{
                        $add_per = false;
                    }
                }
            }

        }
        $this->loadModel( 'Nhom' );
        $arr_Nhom = $this->Nhom->find('list', array(
            'fields' => array('id', 'tennhom'),
            'conditions' => array(
                'Nhom.trangthai' => 'hoatdong'
            ),
            'contain' => false
        ));

        if(isset( $this->data['hidden_nguoidung_nhom_id'] ))
        {
            $this->loadModel( 'Nguoidung' );
            $arr_Nguoidung = $this->Nguoidung->find('list', array(
                'fields' => array('id', 'tendangnhap'),
                'conditions' => array(
                    'Nguoidung.trangthai' => 'hoatdong'
                ),
                'contain' => false
            ));


            $arr_NguoidungNhom = $this->NguoidungNhom->find('all', array(
                'fields' => array('Nhom.id', 'Nhom.tennhom', 'Nguoidung.id', 'Nguoidung.tendangnhap' ),
                'conditions' => array(
                    'Nhom.trangthai' => 'hoatdong',
                    'Nguoidung.trangthai' => 'hoatdong',
                    'nhomid' => $this->data['hidden_nguoidung_nhom_id']
                ),
                'contain' => false
            ));

            $arr_list_id_Nguoidung = array();


            foreach ($arr_NguoidungNhom as $value) {
                $arr_list_id_Nguoidung = $arr_list_id_Nguoidung + array( $value['Nguoidung']['id'] => $value['Nguoidung']['tendangnhap'] );
            }

            $arr_list_id_Nguoidungchuphanquyen = array_diff($arr_Nguoidung, $arr_list_id_Nguoidung);

            $this->set( 'arr_list_id_Nguoidung', $arr_list_id_Nguoidung );
            $this->set( 'arr_list_id_Nguoidungchuphanquyen', $arr_list_id_Nguoidungchuphanquyen );
            $this->set( 'nguoidung_nhom_id',  $this->data['hidden_nguoidung_nhom_id']);
        }

        $this->set( 'arr_Nhom', $arr_Nhom );
        $this->layout = 'admin';
    }

    public function admin_loadnhom( $nhom_id = null ){
        if(!is_numeric($nhom_id)){
            $this->set( 'false', 1 );
        }else{
            $this->set('nguoidung_nhom_id', $nhom_id);
        }
        $this->loadModel( 'Nhom' );
        $arr_Nhom = $this->Nhom->find('list', array(
            'fields' => array('id', 'tennhom'),
            'conditions' => array(
                'Nhom.trangthai' => 'hoatdong'
            ),
            'contain' => false
        ));

        $this->set( 'arr_Nhom', $arr_Nhom );

        $this->loadModel( 'Nguoidung' );
        $arr_Nguoidung = $this->Nguoidung->find('list', array(
            'fields' => array('id', 'tendangnhap'),
            'conditions' => array(
                'Nguoidung.trangthai' => 'hoatdong'
            ),
            'contain' => false
        ));


        $arr_NguoidungNhom = $this->NguoidungNhom->find('all', array(
            'fields' => array('Nhom.id', 'Nhom.tennhom', 'Nguoidung.id', 'Nguoidung.tendangnhap' ),
            'conditions' => array(
                'Nhom.trangthai' => 'hoatdong',
                'Nguoidung.trangthai' => 'hoatdong',
                'nhomid' => $nhom_id
            ),
            'contain' => false
        ));

        $arr_list_id_Nguoidung = array();


        foreach ($arr_NguoidungNhom as $value) {
            $arr_list_id_Nguoidung = $arr_list_id_Nguoidung + array( $value['Nguoidung']['id'] => $value['Nguoidung']['tendangnhap'] );
        }

        $arr_list_id_Nguoidungchuphanquyen = array_diff($arr_Nguoidung, $arr_list_id_Nguoidung);

        $this->set( 'arr_list_id_Nguoidung', $arr_list_id_Nguoidung );
        $this->set( 'arr_list_id_Nguoidungchuphanquyen', $arr_list_id_Nguoidungchuphanquyen );
        $this->set( 'nhom_id', $nhom_id );
        $this->layout = 'ajax';
    }
}
